package com.security;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * AES加密解密算法实现
 */
public class AesImpl implements Securitiable{
    public static final String KEY = "1234567890123456";
    public static final byte[] IV = new byte[16];
    private AES aes ;

    private AesImpl() {
        init();
    }

    private void init(){
        String key = ConfigUtil.getInstance().getByGroup("key","AES");
        aes = new AES(Mode.CTS, Padding.ZeroPadding, key.getBytes(), IV);
    }

    public static AesImpl getInstance() {
        return AesImplHolder.INSTANCE;
    }

    private static class AesImplHolder {
        private static final AesImpl INSTANCE = new AesImpl();
    }

    @Override
    public String encrypt(String text) {
        return aes.encryptBase64(text, StandardCharsets.US_ASCII);
    }

    @Override
    public String decrypt(String text) {
        return aes.decryptStr(Base64.getDecoder().decode(text), StandardCharsets.US_ASCII);
    }
}
